package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.seller.TbGoods;
import com.dongdongshop.pojo.seller.TbGoodsDesc;
import com.dongdongshop.pojo.seller.TbItem;
import com.dongdongshop.pojo.seller.TbItemCat;
import com.dongdongshop.service.*;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service //自己调用自己得service直接放到容器中
public class ItemPageServiceImpl implements ItemPageServiceI {

    //注入freemarker配置类 （配置类在前面已经@Beanl）
    @Autowired  //前三步已经配置到这里面了
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Value("${freemarker.outPath}")
    private String outPath;

    //dubbo引用goodsServiice
    @Reference
    private GoodsService goodsService;
    @Reference
    private GoodsDescService goodsDescService;
    @Reference
    private ItemCatService itemCatService;
    @Reference
    private ItemService itemService;
    @Override
    public void createItemPage(Long goodsId) {
        //创建模板 1.加载配置
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        FileWriter fileWriter=null;
        try {
            //创建模板
            Template template = configuration.getTemplate("item.ftl");
            //创建数据模板
            Map map=new HashMap();
            //调用dubbo接口查商品信息
            TbGoods goods =goodsService.getGoodsById(goodsId);
            //调用dubbo查看商品详情
            TbGoodsDesc goodsDesc =goodsDescService.getGoodsById(goodsId);

            //生成一级二级三级分类
            Long category1Id = goods.getCategory1Id();
            Long category2Id = goods.getCategory2Id();
            Long category3Id = goods.getCategory3Id();
            //查询itecat表
            TbItemCat itemCat1 = itemCatService.getDataById(category1Id);
            TbItemCat itemCat2 = itemCatService.getDataById(category2Id);
            TbItemCat itemCat3 = itemCatService.getDataById(category3Id);

            //根据goodsId 查询出item列表 ，根据SPU查出SKU (详情页面选择规格 点击规格页面会有不同得展示效果)
            // 并以js变量得形式保存到当前页面中
            List<TbItem> itemList =itemService.ListItemsByGoodsId(goodsId);

            map.put("goods",goods);
            map.put("goodsDesc",goodsDesc);
            map.put("itemCat1",itemCat1);
            map.put("itemCat2",itemCat2);
            map.put("itemCat3",itemCat3);
            map.put("itemList",itemList);
            //创建输出流
            fileWriter=new FileWriter(outPath + goodsId + ".html");
            //输出
            template.process(map,fileWriter);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (fileWriter !=null){
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
